package com.yy.bolg.service.impl;

import com.yy.bolg.dao.AdminUserDao;
import com.yy.bolg.entity.AdminUserBean;
import com.yy.bolg.exception.LackParameterException;
import com.yy.bolg.exception.OtherThingsException;
import com.yy.bolg.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 总工Service实现类，对接口里的方法进行构造填充
 * @author enmaoFu
 * @date 2024-10-31
 */
@Service("adminUserService")
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserDao adminUserDao;

    @Override
    public void add(AdminUserBean bean) throws Exception {
        if(StringUtils.isEmpty(bean.getPsAdminUserName())){
            throw new LackParameterException("姓名不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsAdminUserPhone())){
            throw new LackParameterException("姓名不能为空");
        }

        if (null != adminUserDao.findOneByPhone(bean.getPsAdminUserPhone())) {
            throw new OtherThingsException("该手机号已被使用");
        }

        if(StringUtils.isEmpty(bean.getPsAdminUserPwd())){
            throw new LackParameterException("密码不能为空");
        }

        if(StringUtils.isEmpty(bean.getPsAdminUserProvince()) || StringUtils.isEmpty(bean.getPsAdminUserCity()) || StringUtils.isEmpty(bean.getPsAdminUserRegion())){
            throw new LackParameterException("负责辖区不能为空");
        }

        try {
            adminUserDao.add(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("添加数据失败");
        }
    }

    @Override
    public AdminUserBean findOneByPhone(AdminUserBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsAdminUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        try {
            return adminUserDao.findOneByPhone(bean.getPsAdminUserPhone());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂无数据");
        }
    }

    @Override
    public List<AdminUserBean> findAllByCondition(AdminUserBean bean) throws Exception {
        try {
            return adminUserDao.findAllByCondition(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LackParameterException(e.getMessage());
        }
    }

    @Override
    public void del(AdminUserBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsAdminUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (null == adminUserDao.findOneByPhone(bean.getPsAdminUserPhone())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            adminUserDao.del(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("删除数据错误");
        }
    }

    @Override
    public void up(AdminUserBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsAdminUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (null == adminUserDao.findOneByPhone(bean.getPsAdminUserPhone())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            adminUserDao.update(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("修改数据错误");
        }
    }

    @Override
    public AdminUserBean signIn(AdminUserBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsAdminUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (StringUtils.isEmpty(bean.getPsAdminUserPwd())) {
            throw new LackParameterException("密码不能为空");
        }

        AdminUserBean adminUserBean = adminUserDao.findOneByPhone(bean.getPsAdminUserPhone());

        if (null == adminUserBean) {
            throw new OtherThingsException("手机号或密码错误");
        }else{
            if(!bean.getPsAdminUserPhone().equals(adminUserBean.getPsAdminUserPhone()) || !bean.getPsAdminUserPwd().equals(adminUserBean.getPsAdminUserPwd())){
                throw new OtherThingsException("手机号或密码错误");
            }else{
                try {
                    adminUserDao.signIn(bean);
                    return adminUserBean;
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new OtherThingsException("登录失败");
                }
            }
        }
    }

    @Override
    public void signOut(AdminUserBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsAdminUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (null == adminUserDao.findOneByPhone(bean.getPsAdminUserPhone())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            adminUserDao.signOut(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("退出失败");
        }
    }

    @Override
    public void suspend(AdminUserBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsAdminUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (null == adminUserDao.findOneByPhone(bean.getPsAdminUserPhone())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            adminUserDao.suspend(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("暂停权限失败");
        }
    }

    @Override
    public void recovery(AdminUserBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsAdminUserPhone())) {
            throw new LackParameterException("手机号不能为空");
        }

        if (null == adminUserDao.findOneByPhone(bean.getPsAdminUserPhone())) {
            throw new OtherThingsException("暂无数据");
        }

        try {
            adminUserDao.recovery(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("恢复权限失败");
        }
    }
}
